<?php

namespace app\index\controller;

use think\Db;
use think\Validate;
use uploader\Uploader;
use excel\ExcelDownload;
use excel\ExcelReader;

class Ashippingmanagement2 extends Common
{
    //二级出货数据管理
    public function index()
    {
        //查询条件
        $where = $this->request->post();
        $this->assign('where', $where);
        $where = self::str($where);
        $page = empty($where['pages']) ? 1 : $where['pages'];
        $list = empty($where['limts']) ? 10 : $where['limts'];
        unset($where['pages']);
        unset($where['limts']);
        //总数量，总金额，总条数
        $count = Db::table('bx_fy2')->alias('a')
            ->join('cg_jxs b', 'a.jxs_id = b.id')
            ->join('cg_net c', 'a.wd_id = c.id','LEFT')
            ->join('cg_system d', 'a.xt_id = d.id','LEFT')
            ->join('pro_products e', 'a.cp_id = e.id')
            ->join('employee f', 'a.xsUser_id = f.id')
            ->where($where)
            ->count();
        $this->assign('count', $count);
        //分页
        $maxpage = ceil($count / $list);
        $page = $page < $maxpage ? $page : $maxpage;
        $page = $page > 1 ? $page : 1;
        $start = ($page - 1) * $list;
        $this->assign('page', $page);
        $this->assign('maxpage', $maxpage);
        $this->assign('limit', $list);
        //数据
        $data = Db::table('bx_fy2')->alias('a')
            ->join('cg_jxs b', 'a.jxs_id = b.id')
            ->join('cg_net c', 'a.wd_id = c.id','LEFT')
            ->join('cg_system d', 'a.xt_id = d.id','LEFT')
            ->join('pro_products e', 'a.cp_id = e.id')
            ->join('employee f', 'a.xsUser_id = f.id')
            ->field('a.*,b.title jxs,c.title wd,d.title xt,e.pro_name,f.em_name')
            ->where($where)->order('a.id', 'desc')
            ->limit($start, $list)
            ->select();
        $this->assign('data', $data);
        return $this->fetch();
    }

    //替换_
    public function str($where)
    {
        $tmp = [];
        $like = [
            'd_title' => ['like', '%val%'],
            'b_title' => ['like', '%val%'],
            'c_title' => ['like', '%val%'],
            'e_pro_name' => ['like', '%val%'],
            'f_em_name' => ['like', '%val%'],
            'a_lrUser' => ['like', '%val%'],
            'pages' => 'val',
            'limts' => 'val',

        ];
        foreach ($where as $key => $val) {
            if (!empty($val)) {
                if ($where['a_fy2Date'] && $where['a_fy2Date1']) {
                    $tmp['a.fy2Date'] = ['between', [$where['a_fy2Date'], $where['a_fy2Date1']]];
                } else if ($where['a_fy2Date']) {
                    $tmp['a.fy2Date'] = ['>=', $where['a_fy2Date']];
                } else if ($where['a_fy2Date1']) {
                    $tmp['a.fy2Date'] = ['<=', $where['a_fy2Date1']];
                }
                    foreach ($like as $k => $v) {
                        if ($key == $k) {
                            $tmp[preg_replace('/\_/', '.', $key, 1)] = str_replace('val', $val, $v);
                        }
                    }


            }
            unset($tmp['items']);

        }
        return $tmp;
    }

    //新增二级出货页面
    public function addP()
    {
        return $this->fetch('ashippingmanagement2/add');
    }

    //新增二级出货数据
    public function add()
    {
        $data = $this->request->post();
        $validate = new Validate([
            ['fy2Date', 'require|date', '发运日期必须|日期格式'],
            ['jxs', 'require', '经销商必须'],
            ['jxs_id', 'require', '经销商必须'],
            ['shipKind', 'require', '类型必须'],
            ['cp', 'require', '产品名称必须'],
            ['cp_id', 'require', '产品名称必须'],
            ['dw', 'require', '单位必须'],
            ['ghjMoney', 'require|number', '供货价必须|数字格式'],
            ['ghNum', 'require|number', '供货数量必须|数字格式'],
            ['money', 'require|number', '金额必须|数字格式'],
            ['xsUser', 'require', '销售人员必须'],
            ['xsUser_id', 'require', '销售人员必须'],
        ]);
        if ($validate->check($data)) {
            $data['lrUser'] = Db::table('employee')->where('id', session('employee_id'))->value('em_name');
            $data['lrDate'] = date('Y-m-d H:i:s');
            $tmp = $data['file'];unset($data['file']);
            if ($id = Db::table('bx_fy2')->insertGetId($data)) {
                $sdata=["type"=>2,"fydate"=>$data["fy2Date"],"fid"=>$id,"jid"=>$data["jxs_id"],"sid"=>$data["xt_id"],"nid"=>$data["wd_id"],"pid"=>$data["cp_id"],"cman"=>$data["xsUser"]];
                db("bx_fy_huizong")->insert($sdata);
                if (!empty($tmp)) {
                    self::file($tmp, 'bx_fy2', $id);
                }
                return json([
                    "success" => true,
                ]);
            } else {
                return json([
                    "success" => false,
                    'mag' => '网络错误！请重试'
                ]);
            }
        } else {
            return json([
                "success" => $validate->check($data),
                "msg" => $validate->getError()
            ]);
        }
    }
    //编辑
     public function editP(){
        $id = $this->request->get('id');
        if($id){
            $data = Db::table('bx_fy2')->alias('a')
                ->join('cg_jxs b', 'a.jxs_id = b.id')
                ->join('cg_net c', 'a.wd_id = c.id','LEFT')
                ->join('cg_system d', 'a.xt_id = d.id','LEFT')
                ->join('pro_products e', 'a.cp_id = e.id')
                ->join('employee f', 'a.xsUser_id = f.id')
                ->field('a.*,b.title jxs,b.id jxs_id,c.title wd,d.title xt,e.pro_name cp,f.em_name xsUser')
                ->where('a.id',$id)
                ->find();
            $this->assign('data',$data);
            //附件
            $uploadify = self::fileSelect($data['id'],'bx_fy2');
            $this->assign('uploadify',$uploadify);
            return $this->fetch('ashippingmanagement2/edit');
        }else{
            echo '主键错误！';
        }
     }
     //删除单条数据
    public function del(){
        $id = $this->request->get('id');
        if($id){
            if(Db::table('bx_fy2')->where('id',$id)->delete()){
                db("bx_fy_huizong")->where(["fid"=>$id,"type"=>2])->delete();
                $tmp = [];
                self::file($tmp, 'bx_fy2', $id);
                return json([
                    "success" => true
                ]);
            }else{
                return json([
                    "success" => false,
                    'mag' => '网络错误！请重试'
                ]);
            }
        }else{
            echo '主键错误！';
        }
    }
    //详情
    public function infoP(){
        $id = $this->request->get('id');
        if($id){
            $data = Db::table('bx_fy2')->alias('a')
                ->join('cg_jxs b', 'a.jxs_id = b.id')
                ->join('cg_net c', 'a.wd_id = c.id','LEFT')
                ->join('cg_system d', 'a.xt_id = d.id','LEFT')
                ->join('pro_products e', 'a.cp_id = e.id')
                ->join('employee f', 'a.xsUser_id = f.id')
                ->field('a.*,b.title jxs,b.id jxs_id,c.title wd,d.title xt,e.pro_name cp,f.em_name xsUser')
                ->where('a.id',$id)
                ->find();
            $this->assign('data',$data);
            //附件
            $uploadify = self::fileSelect($data['id'],'bx_fy2');
            $this->assign('uploadify',$uploadify);
            return $this->fetch('ashippingmanagement2/info');
        }else{
            echo '主键错误！';
        }
    }
    //导出一级出货数据
    public function export(){
        $user = Db::table('employee')->where('id', session('employee_id'))->value('em_name');
        $str = '注：本表单由销路通软件导出，导出时间：'.date('Y-m-d H:i:s').'；导出人：'.$user.'；金额单位：元';
        $str1 = '二级出货数据汇总';
//        $titles = ['姓名', '年龄', '性别', '身高'];
        // titles格式也可以如下
        $titles = [
            'No' => 'No.',
            'fy2Date' => '日期',
            'jxs' => '供应商',
            'xtwd' => '收货方',
            'cp' => '产品名称',
            'ghjMoney' => '供货价（元）',
            'ghNum' => '数量',
            'dw' => '单位',
            'money' => '金额（元）',
            'phpc' => '批号/批次',
            'des' => '备注',
            'lrUser' => '录入人',
            'lrDate' => '录入时间',
            'xsUser' => '销售人员',
        ];
        Db::query("set @i = 0");//数据库自动增加序号
        $data = Db::table('bx_fy2')->alias('a')
            ->join('cg_jxs b', 'a.jxs_id = b.id')
            ->join('cg_net c', 'a.wd_id = c.id','LEFT')
            ->join('cg_system d', 'a.xt_id = d.id','LEFT')
            ->join('pro_products e', 'a.cp_id = e.id')
            ->join('employee f', 'a.xsUser_id = f.id')
            ->field('(@i:=@i+1) No,a.fy2Date,b.title jxs,case when a.wd="" then a.xt else a.wd end as xtwd,
            e.pro_name cp,a.ghjMoney,a.ghNum,a.dw,a.money,a.phpc,a.des,a.lrUser,a.lrDate,f.em_name xsUser')
            ->select();
        $data[] =  [
            'title' => $str1,
            'sub' => $str,
        ];
        $excel = new ExcelDownload();
        $excel->exec($data, $titles, '二级出货数据', ['sheet' => ['rowHeight' => 20]],3);
    }
    //删除所选数据
    public function delAll()
    {
        $where = $this->request->post('id');
        if (Db::table('bx_fy2')->where("id in ($where)")->delete()) {
            return json([
                "success" => true
            ]);
        } else {
            return json([
                "success" => false,
                'mag' => '网络错误！请重试'
            ]);
        }
    }
     //附件查询处理
    public function fileSelect($id,$table){
        $data = Db::name('annex')->where(['target_id' => $id,'target_table' => $table])->select();
        foreach ($data as $key => $val){
            $val = [
                'filename' => $val['file_name'],
                'uri' => $val['path'],
            ];
            $data[$key]['uri'] = json_encode($val);
        }
        return $data;
    }
     //编辑
    public function edit(){
        $data = $this->request->post();
        $validate = new Validate([
            ['fy2Date', 'require|date', '发运日期必须|日期格式'],
            ['jxs', 'require', '经销商必须'],
            ['jxs_id', 'require', '经销商必须'],
            ['shipKind', 'require', '类型必须'],
            ['cp', 'require', '产品名称必须'],
            ['cp_id', 'require', '产品名称必须'],
            ['dw', 'require', '单位必须'],
            ['ghjMoney', 'require|number', '供货价必须|数字格式'],
            ['ghNum', 'require|number', '供货数量必须|数字格式'],
            ['money', 'require|number', '金额必须|数字格式'],
            ['xsUser', 'require', '销售人员必须'],
            ['xsUser_id', 'require', '销售人员必须'],
        ]);
        if ($validate->check($data)) {
            $data['lrUser'] = Db::table('employee')->where('id', session('employee_id'))->value('em_name');
            $data['lrDate'] = date('Y-m-d H:i:s');
            $tmp = $data['file'];unset($data['file']);
            Db::table('bx_fy2')->where('id',$data['id'])->update($data);
            $sdata=["fydate"=>$data["fy2Date"],"jid"=>$data["jxs_id"],"sid"=>$data["xt_id"],"nid"=>$data["wd_id"],"pid"=>$data["cp_id"],"cman"=>$data["xsUser"]];
            db("bx_fy_huizong")->where(["fid"=>$data['id'],"type"=>2])->update($sdata);
                    self::file($tmp, 'bx_fy2', $data['id']);
            return json([
                "success" => true,
            ]);
        } else {
            return json([
                "success" => $validate->check($data),
                "msg" => $validate->getError()
            ]);
        }
    }
    //附件新增处理
    public function file($data, $table, $id, $ex = '||')
    {
        if (!empty($data)) {
            $data = explode($ex, $data);
            foreach ($data as $k => $v) {
                $tmp = json_decode($v, true);
                $data[$k] = [
                    'target_table' => $table,
                    'target_id' => $id,
                    'path' => $tmp['uri'],
                    'file_name' => $tmp['filename'],
                    's' => 2
                ];
            }
        } else {
            $data = [];
        }
        $file = Db::table('annex')->where(['target_id' => $id,'target_table' => $table])->select();
        foreach ( $file as &$v1){
            $v1['s'] = 0;
            foreach ($data as $k=>&$v2){
                if($v1['path'] == $v2['path']){
                    $v1['s'] = 1;
                    unset($data[$k]);
                }
            }
        }
        $mer = array_merge($file,$data);
        foreach ($mer as $k=>$v){
            if($v['s'] == 0){
                $where2 = [];
                $where2['id'] = $v['id'];
                Db::name('annex')->where($where2)->delete();
            }if($v['s'] == 2){
                unset($v['s']);
                Db::name('annex')->insert($v);
            }
        }
    }

    //上传文件
    public function uploadifive()
    {
        $uploader = new Uploader();
        $data = $uploader->exec($this->request->file());
        $error = $uploader->getError();
        return json([
            'success' => empty($error),
            'message' => $error,
            'data' => $data
        ]);
    }

    //二级出货数据查询
    public function query(){
        //查询条件
        $where = $this->request->post();
        $this->assign('where', $where);
        $where = self::str($where);
        $page = empty($where['pages']) ? 1 : $where['pages'];
        $list = empty($where['limts']) ? 10 : $where['limts'];
        unset($where['pages']);
        unset($where['limts']);
        //总数量，总金额，总条数
        $count = Db::table('bx_fy2')->alias('a')
            ->join('cg_jxs b', 'a.jxs_id = b.id')
            ->join('cg_net c', 'a.wd_id = c.id','LEFT')
            ->join('cg_system d', 'a.xt_id = d.id','LEFT')
            ->join('pro_products e', 'a.cp_id = e.id')
            ->join('employee f', 'a.xsUser_id = f.id')
            ->where($where)
            ->count();
        $this->assign('count', $count);
        //分页
        $maxpage = ceil($count / $list);
        $page = $page < $maxpage ? $page : $maxpage;
        $page = $page > 1 ? $page : 1;
        $start = ($page - 1) * $list;
        $this->assign('page', $page);
        $this->assign('maxpage', $maxpage);
        $this->assign('limit', $list);
        //数据
        $data = Db::table('bx_fy2')->alias('a')
            ->join('cg_jxs b', 'a.jxs_id = b.id')
            ->join('cg_net c', 'a.wd_id = c.id','LEFT')
            ->join('cg_system d', 'a.xt_id = d.id','LEFT')
            ->join('pro_products e', 'a.cp_id = e.id')
            ->join('employee f', 'a.xsUser_id = f.id')
            ->field('a.*,b.title jxs,c.title wd,d.title xt,e.pro_name,f.em_name')
            ->where($where)->order('a.id', 'desc')
            ->limit($start, $list)
            ->select();
        $this->assign('data', $data);
        return $this->fetch('ashippingmanagement2/query');
    }
    //导入文件
    public function import(){
        return $this->fetch();
    }
    //导入
    public function upload()
    {
        // 获取表单上传文件
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/bx_fy 目录下
        $info = $file->validate(['ext' => 'xls,xlsx', 'size' => 105678])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'bx_fy2');
        if ($info) {
            // 成功上传后 获取上传信息
            $data = self::testExcel2($info->getSaveName());
            echo "<script>top.sweetAlert('" . $data['msg'] . "','','" . $data['success'] . "');parent.location.reload();</script>";
        } else {
            // 上传失败获取错误信息
            echo "<script>top.sweetAlert('" . $file->getError() . "','','warning');</script>";
        }
    }
    /**
     * excel导入示例
     */
    public function testExcel2($file)
    {
        $url = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'bx_fy2' . DS . $file;
        $reader = new ExcelReader($url);
        $titlesMap = [
            'fy2Date' ,// '日期',
            'jxs_no' ,// '经销商编号',
            'jxs' ,// '经销商名称',
            'xt_no' ,// '系统编号',
            'xt' ,// '系统名称',
            'wd_no' ,// '网点编号',
            'wd' ,// '网点名称',
            'cp_no' ,// '产品编号',
            'cp' ,// '产品名称',
            'ghjMoney' ,// '供货价(元)',
            'ghNum' ,// '数量',
            'dw' ,// '单位',
            'money' ,// '金额(元)',
            'xsUser' ,// '销售人员',
            'phpc' ,// '批号/批次',
            'des' ,// '备注',
        ];
        // 是否提供titlesMap 返回的数据结构不一致
        $data = $reader->exec();
        if ($data) {
            unset($data[0]);
            unset($data[1]);
            unset($data[2]);
            unset($data[3]);
            foreach ($data as $key => $val) {
                $is_empty = 0;
                foreach ($val as $k => $v) {
                    if (!trim($v)) {
                        $is_empty++;
                    }
                }
                if ($is_empty == count($val)) unset($data[$key]);
            }
            foreach ($data as $key => $val){
                $tmp[] = array_combine($titlesMap, $val);
            }
//            $tmp =  [
//                0 => [
//                    'fy2Date' => '2018-02-01',
//                    'jxs_no' => '1',
//                    'jxs' => '第一个经销商',
//                    'xt_no' => '1',
//                    'xt' => '第一个系统',
//                    'wd_no' => '1',
//                    'wd' => '网点1',
//                    'cp_no' => 'PRO001',
//                    'cp' => '测试2',
//                    'ghjMoney' => '80',
//                    'ghNum' => '100',
//                    'dw' => '支',
//                    'money' => '80000',
//                    'xsUser' => '测试',
//                    'phpc' => 'SA0001',
//                    'des' => '',
//                ],
//                1 => [
//                    'fy2Date' => '2018-02-01',
//                    'jxs_no' => '1',
//                    'jxs' => '第一个经销商',
//                    'xt_no' => '1',
//                    'xt' => '第一个系统',
//                    'wd_no' => '1',
//                    'wd' => '网点1',
//                    'cp_no' => 'PRO001',
//                    'cp' => '测试2',
//                    'ghjMoney' => '80',
//                    'ghNum' => '100',
//                    'dw' => '支',
//                    'money' => '80000',
//                    'xsUser' => '测试',
//                    'phpc' => 'SA0001',
//                    'des' => '',
//                ],
//            ];
            if ($tmp) {
                $lrUser = Db::table('employee')->where('id', session('employee_id'))->value('em_name');
                //经销商
                $jx = Db::table('cg_jxs')->field('id,sn,title')->select();
                $title1 = array_column($jx,'title');
                $jx1 = implode(',', $title1);
                $jx_no = implode(',', array_column($jx,'sn'));
                $jx2 =  array_combine(array_column($jx,'id'),$title1);
                //系统
                $xt = Db::table('cg_system')->field('id,sn,title')->select();
                $title2 = array_column($xt,'title');
                $xt1 = implode(',', $title2);
                $xt_no = implode(',', array_column($xt,'sn'));
                $xt2 =  array_combine(array_column($xt,'id'),$title2);
                //产品
                $cp = Db::table('pro_products')->field('id,pro_no,pro_name')->select();
                $title3 = array_column($cp,'pro_name');
                $cp1 = implode(',',$title3);
                $cp_no = implode(',', array_column($cp,'pro_no'));
                $cp2 =  array_combine(array_column($cp,'id'),$title3);
                //网点
                $wd = Db::table('cg_net')->field('id,sn,title')->select();
                $title4 = array_column($wd,'title');
                $wd1 = implode(',', $title4);
                $wd_no = implode(',', array_column($wd,'sn'));
                $wd2 =  array_combine(array_column($wd,'id'),$title4);
                //销售人员
                $ry = Db::table('employee')->field('id,em_name')->select();
                $title5 = array_column($ry,'em_name');
                $ry1 = implode(',', $title5);
                $ry2 =  array_combine(array_column($ry,'id'),$title5);
                //时间范围
                $firstDate = date('Y-m-') . '01';
                $lastDate = date('Y-m-d', strtotime("$firstDate +1 month -1 day"));
                $validate = new Validate([
                    ['fy2Date', 'require|date|between:' . $firstDate . ',' . $lastDate . '', '发运日期必须|日期格式|发运日期必须在' . $firstDate . '之间' . $lastDate],
                    ['jxs', 'require|in:' . $jx1, '经销商必须|经销商不存在'],
                    ['jxs_no', 'require|in:' . $jx_no, '经销商编号必须|经销商编号不存在'],
                    ['xt', 'require|in:' . $xt1, '系统名称必须|系统名称不存在'],
                    ['xt_no', 'require|in:' . $xt_no, '系统编号必须|系统编号不存在'],
                    ['cp', 'require|in:' . $cp1, '产品名称必须|产品不存在'],
                    ['cp_no', 'require|in:' . $cp_no, '产品编号必须|产品编号不存在'],
                    ['wd', 'require|in:' . $wd1, '网点名称必须|网点不存在'],
                    ['wd_no', 'require|in:' . $wd_no, '网点编号必须|网点编号不存在'],
                    ['ghjMoney', 'require|number', '供货价必须|数字格式'],
                    ['ghNum', 'require|number', '必须|数数量字格式'],
                    ['money', 'require|number', '金额必须|数字格式'],
                    ['dw', 'require', '单位必须'],
                    ['xsUser', 'require|in:' . $ry1, '销售人员必须|销售人员不存在'],
                ]);
                $off = 1;
                foreach ($tmp as $key => $val) {
                    $tmp[$key]['jxs_id'] = array_search($tmp[$key]['jxs'], $jx2);
                    $tmp[$key]['cp_id'] = array_search($tmp[$key]['cp'], $cp2);
                    $tmp[$key]['wd_id'] = array_search($tmp[$key]['wd'], $wd2);
                    $tmp[$key]['xsUser_id'] = array_search($tmp[$key]['xsUser'], $ry2);
                    $tmp[$key]['lrUser'] = $lrUser;
                    $tmp[$key]['lrDate'] = date('Y-m-d H:i:s');
                    if ($validate->check($val)) {
                        $off = 1;
                    } else {
                        $off = 0;
                        break;
                    }
                }
                if ($off === 1) {
                    $log = [
                        'operator' => $lrUser,
                        'log' => '导入了' . date('Y日m月') . '的发运数据',
                        'file' => $file,
                        'date' => date('Y-m-d H:i:s'),
                        'ny' => date('Y-m'),
                    ];
                    if (Db::table('bx_fy2')->insertAll($tmp)) {
                        if (Db::table('bx_fy2_log')->insert($log)) {
                            return [
                                "success" => 'success',
                                'msg' => '上传文件成功'
                            ];
                        } else {
                            return [
                                "success" => 'warning',
                                'msg' => '网络错误！请重试'
                            ];
                        }
                    } else {
                        return [
                            "success" => 'warning',
                            'msg' => '网络错误！请重试'
                        ];
                    }
                } else {
                    return [
                        "success" => 'warning',
                        "msg" => $validate->getError()
                    ];
                }
            } else {
                return [
                    "success" => 'warning',
                    'msg' => '请填充数据'
                ];
            }
        } else {
            return [
                "success" => 'warning',
                'msg' => '网络错误！请重试'
            ];
        }
    }
    //一级出货数据日志
    public function log()
    {
        $where = $this->request->post();
        $this->assign('where', $where);
        $where = self::str1($where, 'date');
        $page = empty($where['pages']) ? 1 : $where['pages'];
        $list = empty($where['limts']) ? 10 : $where['limts'];
        unset($where['pages']);
        unset($where['limts']);
        //总条数
        $count = Db::table('bx_fy2_log')->where($where)->count();
        $this->assign('count', $count);
        //分页
        $maxpage = ceil($count / $list);
        $page = $page < $maxpage ? $page : $maxpage;
        $page = $page > 1 ? $page : 1;
        $start = ($page - 1) * $list;
        $this->assign('page', $page);
        $this->assign('maxpage', $maxpage);
        $this->assign('limit', $list);
        //数据
        $data = Db::table('bx_fy2_log')
            ->where($where)->order('id', 'desc')
            ->limit($start, $list)
            ->select();
        $this->assign('data', $data);
        return $this->fetch();
    }
    //log处理数组
    public function str1($where, $field)
    {
        $where = array_filter($where);
        if (isset($where['start']) && isset($where['end'])) {
            $firstDate = date('Y-m-d H:i:s', strtotime($where['start']));
            $lastDate = date('Y-m-d H:i:s', strtotime($where['end']));
            $where[$field] = ['between', [$firstDate, $lastDate]];
        } else if (isset($where['start'])) {
            $firstDate = date('Y-m-d H:i:s', strtotime($where['start']));
            $lastDate = '2050-12-31 23:59:59';
            $where[$field] = ['between', [$firstDate, $lastDate]];
        }else if (isset($where['end'])) {
            $firstDate = '1970-12-31 23:59:59';
            $lastDate = date('Y-m-d H:i:s', strtotime($where['end']));
            $where[$field] = ['between', [$firstDate, $lastDate]];
        }
        unset($where['start']);
        unset($where['end']);
        return $where;
    }

}
